package model;

import connection.DbConnection;
import bean.Poll;
import bean.PollUser;
import bean.Question;
import java.sql.*;
import java.util.ArrayList;

public class PollImpl {
	
	public void setPollAttributesPM (ArrayList<Poll> array, String username) {
		try {
			ResultSet rsOne = null;
			DbConnection conn = new DbConnection();
			// Needs updating after James changes DB to have Poll_PollMaster table
			rsOne = conn.doSelect("SELECT * FROM User_Poll where Username = '"+username+"'");
			while (rsOne.next()) {
				ResultSet rsTwo = null;
				rsTwo = conn.doSelect("SELECT * FROM Poll where ID = '"+rsOne.getInt("PollID")+"'");
				Poll p = new Poll();
				p.setId(rsTwo.getInt("ID"));
				p.setName(rsTwo.getString("Name"));
				p.setDescription(rsTwo.getString("Description"));
				p.setCreatetime(rsTwo.getDate("CreateTime"));
				p.setLatitude(rsTwo.getString("Latitude"));
				p.setLongitude(rsTwo.getString("Longitude"));
				p.setCreator(rsTwo.getString("CreatorName"));
				array.add(p);
			}
		} catch (NullPointerException npe) {
			npe.printStackTrace();
			System.out.println("******** Null Pointer Exception when executing setUserAttributes method.");
			System.out.println("******** Ie. Your user bean hasn't been created!");
		} catch (SQLException sqle) {
			sqle.printStackTrace();
			System.out.println("******** SQL problem when executing setUserAttribute method.");
			System.out.println("******** Ie. Your sql query is wrong!");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void setPollAttributes (Poll poll) {
		try {
			ResultSet resultSet = null;
			DbConnection conn = new DbConnection();
			resultSet = conn.doSelect("SELECT * FROM POLL where ID = '" + poll.getId() + "'");
			resultSet.next();
			
			poll.setVersion(resultSet.getInt("Version"));
			poll.setLatitude(resultSet.getString("Latitude"));
			poll.setLongitude(resultSet.getString("Longitude"));
			poll.setCreator(resultSet.getString("CreatorName"));
			poll.setName(resultSet.getString("Name"));
			poll.setDescription(resultSet.getString("Description"));
			
		} catch (NullPointerException npe) {
			npe.printStackTrace();
			System.out.println("******** Null Pointer Exception when executing setPollAttribute method.");
			System.out.println("******** Ie. Your poll bean hasn't been created!");
		} catch (SQLException sqle) {
			sqle.printStackTrace();
			System.out.println("******** SQL problem when executing setPollAttribute method.");
			System.out.println("******** Ie. Your sql query is wrong!");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
